package com.iflytek.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iflytek.entity.Dept;
import com.iflytek.entity.User;
import com.iflytek.service.DeptService;
import com.iflytek.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

//用户管理模块的控制器
@Controller
public class DepartmentController {

    @Autowired
    private DeptService departmentService;

    @Autowired
    private UserService userService;

    //所有部门内信息展示
    @GetMapping("/departmentList")
    public String listUser(Model model) {
        QueryWrapper<Dept> w = new QueryWrapper<Dept>().eq("del","0");
        List<Dept> departmentList = departmentService.list(w);
        model.addAttribute("departmentList", departmentList);
        return "departmentList";
    }

    //部门详细信息
    @GetMapping("/departmentDetail")
    //SpringBoot提供的一个Model对象，是Controller和页面之间的一个桥梁
    //要使用Model，直接在方法上声明Model就可以使用
    public String showUserDetail(Model model, String code) {
        Dept department = departmentService.getById(code);
        model.addAttribute("department", department);
        Wrapper<User> w = new QueryWrapper<User>().eq("dept_code",department.getDeptCode());
        List<User> userList = userService.list(w);
        model.addAttribute("userList", userList);

        return "departmentDetail";
    }

    //物理删除部门
    @GetMapping("departmentDelete")
    public String deleteDepartment(String code) {
        departmentService.removeById(code);
        return "redirect:departmentList";
    }

    //逻辑删除部门
    @GetMapping("/departmentMark")
    public String markDepartment(String code) {
        departmentService.markDept(code);
        return "redirect:departmentList";
    }

    //跳到修改部门页面
    @GetMapping("/departmentEdit")
    public String editDepartment(String code, Model model) {
        QueryWrapper<Dept> w = new QueryWrapper<Dept>().eq("dept_code",code);
        Dept department = departmentService.getOne(w);
        model.addAttribute("department", department);
        return "departmentEdit";
    }

    //保存修改部门信息
    @PostMapping("/saveDept")
    public String saveDept(Dept department, Model model) {
        String msg;
        try {
            departmentService.saveDept(department);
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
            model.addAttribute("msg", msg);
        }
        msg = "修改完成";
        model.addAttribute("msg", msg);
        return "departmentEdit";
    }

    //前往新增部门页面
    @GetMapping("/createDept")
    public String toCreateDept(Model model) {
        return "departmentCreate";
    }

    //保存新增部门
    @PostMapping("/insertDept")
    public String createDept(Dept department, Model model) {
        try {
            departmentService.createDept(department);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("msg", e.getMessage());
            return "departmentCreate";
        }
        return "redirect:departmentList";

    }

}
